<template>
	<view class="index-wrap">
		<view class="index-bg" style="height: 240rpx;"></view>
		<view class="index-search">
			<view class="index-search-box" @click="gotoRouter('/pages/search/search')">
				<view class="search-box-layer">
					<image class="search-box-icon" src="http://k8s.vendingtech.vip/images/search@2x.png"></image>
					<view class="search-box-info">搜索医院/医生/疾病/知识</view>
				</view>
			</view>
			<image class="index-search-infotm" src="http://k8s.vendingtech.vip/images/inform_default@2x.png" @click="gotoRouter('/pages/message/message')"></image>
		</view>

		<!-- <image class="index-ikon" src="http://k8s.vendingtech.vip/images/banner.png"></image> -->
		<view class="index-ikon">
			<u-banner ></u-banner>
		</view>
		

		<view class="index-inquiry">
			<view class="index-inquiry-main" @click="gotoRouter('/pages/findDoctor/findDoctor')">
				<view class="index-inquiry-section">
					<image src="http://k8s.vendingtech.vip/images/specialist_iccon@2x.png" class="pic2-0"></image>
					<view>专家问诊</view>
				</view>
			</view>
			<!-- <view class="index-inquiry-main" @click="gotoRouter('/pages-inquiry/inquirySelect/inquirySelect?type=1')">
				<view class="index-inquiry-section">
					<image src="http://k8s.vendingtech.vip/images/inquiry_icon@2x.png" class="pic2-1"></image>
					<view>极速问诊</view>
				</view>
			</view> -->
			
			<view class="index-inquiry-main" @click="gotoRouter('/pages/search/search')">
				<view class="index-inquiry-section">
					<image src="http://k8s.vendingtech.vip/images/hospital_icon2@2x.png" class="pic2-1"></image>
					<view>医院查找</view>
				</view>
			</view>
		</view>
		<view class="index-inquiry">
			<view class="index-inquiry-main" @click="gotoRouter('/pages-hospital/illnessList/illnessList')">
				<view class="index-inquiry-section">
					<image src="http://k8s.vendingtech.vip/images/medical_icon@2x.png" class="pic2-1"></image>
					<view>疾病查找</view>
				</view>
			</view>
			<view class="index-inquiry-main" @click="gotoRouter('/pages-hospital/articleList/articleList')">
				<view class="index-inquiry-section">
					<image src="http://k8s.vendingtech.vip/images/book_icon@2x.png" class="pic2-1"></image>
					<view>医院公示</view>
				</view>
			</view>
		</view>

		<!-- <view class="index-tab">
			<view class="index-tab-outer" @click="gotoRouter('/pages-inquiry/inquirySelect/inquirySelect?type=2')">
				<image src="http://k8s.vendingtech.vip/images/prescription_icon@2x.png" class="index-tab-pic"></image>
				<view>开药问诊</view>
			</view>
			<view class="index-tab-outer" @click="gotoRouter('/pages/search/search')">
				<image src="http://k8s.vendingtech.vip/images/hospital_icon2@2x.png" class="index-tab-pic"></image>
				<view>医院查找</view>
			</view>
			<view class="index-tab-outer" @click="gotoRouter('/pages-hospital/illnessList/illnessList')">
				<image src="http://k8s.vendingtech.vip/images/medical_icon@2x.png" class="index-tab-pic"></image>
				<view>疾病查找</view>
			</view>
			<view class="index-tab-outer" @click="gotoRouter('/pages-hospital/articleList/articleList')">
				<image src="http://k8s.vendingtech.vip/images/book_icon@2x.png" class="index-tab-pic"></image>
				<view>健康知识</view>
			</view>
		</view> -->

		<view class="index-doctor" v-if="doctorList.length > 0">
			<u-title value="推荐医生"></u-title>
			<view class="index-doctor-scroll scrollX">
				<view class="doctor-scroll-outer" v-for="(item, index) in doctorList" :key="index" @click="gotoRouter('/pages-hospital/doctorInfo/doctorInfo?id=' + item.id)">
					<view class="doctor-scroll-outer-top">
						<view class="block1"><image :src="item.picture" mode="aspectFill"></image></view>
						<view class="block2">
							<view class="wrap1">
								<text lines="1" class="info2">{{ item.doctorName }}</text>
								<text lines="1" class="info3">{{ item.jobGradeName || '' }}</text>
							</view>
							<view class="info4 nowrap">{{ item.hospName }}&nbsp;{{ item.departmentName }}</view>
						</view>
					</view>

					<view class="doctor-scroll-outer-bottom">
						<view>擅长：</view>
						<text>{{ item.goodsDirection }}</text>
					</view>
				</view>
			</view>
		</view>

		<view class="index-article" v-if="articleList.length > 0">
			<u-title value="医院公示"></u-title>
			<view class="index-article-scroll "><u-article-item v-for="(item, index) in articleList" :key="index" :data="item"></u-article-item></view>
		</view>

		<!-- <view class="end-line">～我是有底线的呢～</view> -->
	</view>
</template>

<script>
import uArticleItem from '@/components/u-article-item/u-article-item.vue';
import uTitle from '@/components/u-title/u-title.vue';
import uBanner from '@/components/u-banner/u-banner.vue';
import { doctorInfo, currentImInfo } from '@/utils/token.js';
const app = getApp();
export default {
	components: {
		uArticleItem,
		uTitle,
		uBanner
	},
	data() {
		return {
			doctorList: [],
			articleList: []
		};
	},
	onLoad(options) {
		this.getUserDetail();
	},
	onShow() {
		//异常回到主页
		// if (app.globalData.role != 0) {
		// 	app.globalData.role = 0;
		// 	app.globalData.nim.destroy({
		// 		done: () => {
		// 			this.initIm(imInfo.get().token, imInfo.get().accid);
		// 			uni.reLaunch({
		// 				url: '/pages/index/index'
		// 			});
		// 		}
		// 	});
		// }
		this.queryMpInfo();
		this.queryDoctorList();
		this.queryArticleList();
	},
	methods: {
		queryMpInfo() {
			this.$api.getMpConfig().then(res => {
				if (res.code == 200) {
					uni.setNavigationBarTitle({
						title: res.result.contentInfo.plantName //这是修改后的导航栏文字
					})
				}
			})
		},
		//获取用户详情
		getUserDetail() {
			this.$api.getUserDetail({}).then(res => {
				if (res.code == 200) {
					this.currentRole = res.result?.currentRole;
				
					this.getDoctorInfo();
				}
			});
		},

		getDoctorInfo() {
			this.$api.getDoctorInfo({}).then(res => {
				if (res.code == 200) {
					// let token = res.result?.imToken;
					// let accid = res.result?.id;
					
					if (this.currentRole == 1) {
						// 医生
						app.globalData.role = 1;
						/*
						app.globalData.nim.destroy({
							done: () => {
								this.initIm(token, accid);
								uni.reLaunch({
									url: '/pages/doctor/doctor'
								});
							}
						});
						*/
						doctorInfo.set(res.result, Date.parse(new Date()) + (86400000 * 29))
						// 获取医生/药师的腾讯云IM信息
						this.$api.doctorTxyImInfo({
							params: {
								id: res.result.id
							}
						}).then(res_ => {	
							/*
							app.globalData.tim.destroy().then(() => {
								currentImInfo.set({
									sdkAppId: res_.result.sdkAppId,
									id: res_.result.id,
									userSig: res_.result.userSig
								}, Date.parse(new Date()) + (86400000 * 29));
								this.initTxyIm(res_.result.sdkAppId, res_.result.id, res_.result.userSig);
								uni.reLaunch({
									url: '/pages/doctor/doctor'
								});
							})
							*/
							currentImInfo.set({
								sdkAppId: res_.result.sdkAppId,
								id: res_.result.id,
								userSig: res_.result.userSig
							}, Date.parse(new Date()) + (86400000 * 29));
							getApp().globalData.txyImController.logout();
							getApp().globalData.txyImController.login(res_.result.id, res_.result.userSig);
							uni.reLaunch({
								url: '/pages/doctor/doctor'
							});
						})
					} else if (this.currentRole == 2) {
						// 药师
						app.globalData.role = 2;
						/*
						app.globalData.nim.destroy({
							done: () => {
								this.initIm(token, accid);
								uni.reLaunch({
									url: '/pages-doctor/pharmacist/pharmacist'
								});
							}
						});
						*/
						// 获取医生/药师的腾讯云IM信息
						this.$api.doctorTxyImInfo({
							params: {
								id: res.result.id
							}
						}).then(res_ => {
							currentImInfo.set({
								sdkAppId: res_.result.sdkAppId,
								id: res_.result.id,
								userSig: res_.result.userSig
							}, Date.parse(new Date()) + (86400000 * 29));
							getApp().globalData.txyImController.logout();
							getApp().globalData.txyImController.login(res_.result.id, res_.result.userSig);
							uni.reLaunch({
								url: '/pages-doctor/pharmacist/pharmacist'
							});
						})
					} else if (this.currentRole == 3) {
						// 机构
						app.globalData.role = 3;
						uni.reLaunch({
							url: '/pages-agencies/agencies/agencies'
						});
					}
				} else {
					uni.showToast({
						title: '身份切换失败',
						icon: 'none'
					});
				}
			});
		},

		//医生列表
		queryDoctorList() {
			this.$api
				.queryDoctorList({
					params: {
						isHome: 1,
						pageSize: 30
					}
				})
				.then(res => {
					if (res.code == 200) {
						this.doctorList = res.result.records;
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
					}
				});
		},

		//文章列表
		queryArticleList() {
			this.$api
				.queryArticleList({
					params: {
						isReferrer: 1,
						pageSize: 100
					}
				})
				.then(res => {
					if (res.code == 200) {
						this.articleList = res.result.records;
					} else {
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
					}
				});
		}
	}
};
</script>

<style lang="less">
@import './index.less';
</style>
